<?php
/**
 * 用户信息(待改)
 */
class User extends Model{
	protected $table_name = 'v_user';
	
	public function __construct() {
	    parent::__construct();
	}
	
	private function setSession($userInfo) {
	    $_SESSION[$GLOBALS['app_id']]['userInfo'] = $userInfo;
	}
	
	private function getSession() {
	    $info = $_SESSION[$GLOBALS['app_id']]['userInfo'];
	    return $info;
	}
	
	private function deleteSession() {
	    $_SESSION[$GLOBALS['app_id']]['userInfo'] = array();
	}
	

	public function setLoginInfo($user_id, $yyuid, $username, $login_type='yy'){
		if( empty($user_id)||empty($username)||empty($login_type) ) return false;
		$userInfo = array(
			'user_id'=>$user_id,
			'yyuid'=>$yyuid,
			'username'=>$username,
			'login_type'=>$login_type,
		);
		
		$this->setSession($userInfo);
		setcookie(COOKIE_KEY_LOGIN_USERNAME, $username, 0, '/');
		setcookie(COOKIE_KEY_LOGIN_TYPE,     $login_type, 0, '/');
		
		return $userInfo;
	}
	
	public function getLoginInfo(){
	    $info = $this->getSession();
	    
		if(!empty($info)&&!empty($info['user_id'])&&!empty($info['username']) ){
			if( ('yy'==$info['login_type']&&$info['username']==$_COOKIE['username'])
			   ||('yy'!=$info['login_type']&&$info['username']==$_COOKIE[COOKIE_KEY_LOGIN_USERNAME])){
				return $info;
			} else { 
			    $this->deleteSession();
			}
		}

		//检测yy是否登录
		if( !empty($_COOKIE['username']) ) {
			$userInfo = obj('dwUDB')->isLogin();
			if(!empty($userInfo)&&!empty($userInfo['yyuid'])){
				$user = $this->find( array('user_id'=>$userInfo['yyuid']) );
				if( empty($user) ){
					$data = array(
						'user_id'=>$userInfo['yyuid'],
						'udb'=>'',
						'nickname'=>'',
						'approve_name' => '',
						'sex' => 0,
						'role' => 1,
						'created' => time(),
					);
					$user_id = $this->insert($data);
				}else{
					//UPDATE操作先省略
					$user_id = $user['user_id'];
				}
				if ($user_id) {
					return $this->setLoginInfo($user_id, $userInfo['yyuid'], $userInfo['username'], 'yy');
				}
			}
		}
		return array('user_id'=>0, 'yyuid'=>0);
	}
	
	public function clearLoginInfo(){
	    $this->deleteSession();
		setcookie(COOKIE_KEY_LOGIN_USERNAME, '', time()-3600, '/'); 
		setcookie(COOKIE_KEY_LOGIN_TYPE,     '', time()-3600, '/');
		setcookie('username', '', time()-3600, '/', $_SERVER['HTTP_HOST']);
	}

	//用户是否存在
	public function isExist( array $conditons ){
	    $rs = $this->select($conditons);
	    return !empty($rs);
	}
	
	// 登录
	public function login( $callbackUrl, $title = 'Huya CMS') {
	    echo '<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>'. $title .'</title><script type="text/javascript" src="http://assets.dwstatic.com/b=common/js&f=jquery-1.10.2.min.js,jquery-migrate-1.2.1.min.js,dwudbproxy.js&20141030"></script><script type="text/javascript">$(window).on("load", function(){dwUDBProxy.login("'.$callbackUrl.'");});</script></head><body></body></html>'; 
	    exit;
	}
	
	// 登出
	public function logout( $callbackUrl ){
	    @session_start();
	    unset($_SESSION[$GLOBALS['app_id']]);
	    echo '<!doctype html><html><head><script src="http://assets.dwstatic.com/b=common/js&f=jquery-1.10.2.min.js,jquery-migrate-1.2.1.min.js,dwudbproxy.js&20141030"></script></head><body><script type="text/javascript">$(window).on("load", function(){dwUDBProxy.logout("'.$callbackUrl.'");});</script></body></html>';
	    exit;
	}
	
	//登录验证失败跳转
	public function errorJumpOut( $callbackUrl ) {
	    //header("HTTP/1.0 403");
	    echo '<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>function tips(){alert("您还没有登录或登录已过期，请登录！");top.location.href="'.$callbackUrl .'"}</script></head><body onLoad="tips()"></body></html>';
	    exit;
	}
	
}